package org.lyimg.com.imgapplication.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.LazyHeaders;

import org.lyimg.com.imgapplication.R;
import org.lyimg.com.imgapplication.activity.ImgsActivity;
import org.lyimg.com.imgapplication.bean.ImgInfo;
import org.lyimg.com.imgapplication.bean.ImgType;
import org.lyimg.com.imgapplication.utils.PhotoDialog;
import org.lyimg.com.imgapplication.utils.httpUtils;

import java.util.List;

/**
 * Created by liuyong on 2017/4/19.
 */

public class ImgDetailRecAdapter extends RecyclerView.Adapter<ImgDetailRecAdapter.MyViewHolder> {

    Context context;
    List<ImgInfo> list;

    public ImgDetailRecAdapter(Context context, List<ImgInfo> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_img, parent, false));
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Log.i("TAG", "onBindViewHolder: " + list.get(position).src);
        Glide.with(context).load(buildGlideUrl(list.get(position).src)).diskCacheStrategy(DiskCacheStrategy.ALL).fitCenter()
                .crossFade().into(holder.img);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        ImageView img;
        public MyViewHolder(View view) {
            super(view);
            img = (ImageView) view.findViewById(R.id.img);
            img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    new PhotoDialog(context,list.get(getPosition()).src).show();
                }
            });
        }
    }
    private GlideUrl buildGlideUrl(String url) {
        if (TextUtils.isEmpty(url)) {
            return null;
        } else {
            return new GlideUrl(url, new LazyHeaders.Builder()
                    .addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
                    .addHeader("Accept-Encoding", "gzip, deflate, sdch, br")
                    .addHeader("Accept-Language", "zh-CN,zh;q=0.8")
                    .addHeader("Cache-Control", "max-age=0")
                    .addHeader("Connection", "keep-alive")
                    .addHeader("If-None-Match", "5c45772c-562c")
                    .addHeader("Upgrade-Insecure-Requests", "1")
                    .addHeader("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1")
                    .build());
        }
    }
}
